package com.haois.mp.database.component.init;

import com.alibaba.druid.pool.DruidDataSource;
import com.haois.mp.database.commom.properties.DefaultDruidProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


/**
 * Created by RuiHaoZ on GCU
 *
 * @author RuiHaoZ
 * @date 2017/11/22
 */
@Configuration
public class SQLConfiguration {

    @Bean
    public DruidDataSource dataSource(DefaultDruidProperties defaultDruidProperties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        defaultDruidProperties.config(druidDataSource);
        return druidDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DruidDataSource druidDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(druidDataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager(DruidDataSource druidDataSource) {
        return new DataSourceTransactionManager(druidDataSource);
    }
}
